<?php
/**
 * Created by VISPHP 快速开发框架.
 * Author:乐清
 * email:804966813@qq.com
 * Date: 2019/2/18
 * Time: 10:22
 */

namespace app\admin\controller;

use app\admin\model\Authmenu;
use think\Db;
use app\admin\model\Membercate;
class Role extends Visphp
{
    public function index(){
        if(request()->isAjax()){
            $db=new Membercate();
            $list=$db->order('create_time','asc')->select();

            return json($list);
        }
        return view();
    }
    public function preview()
    {
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $model = new Membercate();
        $info['cate'] = $model->where('id',$id)->find();
        if(!empty($info['cate']['permissions'])) {
            //将菜单id字符串拆分成数组
            $info['cate']['permissions'] = explode(',',$info['cate']['permissions']);
        }
        $menus = Db::name('authmenu')->select();
        $info['menu'] = $this->mblist($menus);
        $this->assign('info',$info);
        return $this->fetch();

    }

    protected function mblist($menu,$id=0,$level=0){

        static $menus = array();
        $size = count($menus)-1;
        foreach ($menu as $value) {
            if ($value['pid']==$id) {
                $value['level'] = $level+1;
                if($level == 0)
                {
                    $value['str'] = str_repeat('',$value['level']);
                    $menus[] = $value;
                }
                elseif($level == 2)
                {
                    $value['str'] = '&emsp;&emsp;&emsp;&emsp;'.'└ ';
                    $menus[$size]['list'][] = $value;
                }
                elseif($level == 3)
                {
                    $value['str'] = '&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;'.'└ ';
                    $menus[$size]['list'][] = $value;
                }
                else
                {
                    $value['str'] = '&emsp;&emsp;'.'└ ';
                    $menus[$size]['list'][] = $value;
                }

                $this->mblist($menu,$value['id'],$value['level']);
            }
        }
        return $menus;
    }
    public function delete(){
        if($this->request->isAjax()) {
            $model = new Membercate();
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if($id > 0) {
                if($id == 1) {
                     $this->error('超级管理员角色不能删除');
                }
                if(false == $model->where('id',$id)->delete()) {
                    $this->error('删除失败');
                } else {

                     $this->success('删除成功','admin/role/index');
                }
            } else {
                 $this->error('id不正确');
            }
        }
    }
    public function edit()
    {
        //获取角色id
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $model = new Membercate();
        $menuModel = new Authmenu();
        if($id > 0) {
            //是修改操作
            if($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['name', 'require', '角色名称不能为空'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                //验证用户名是否存在
                $name = $model->where(['name'=>$post['name'],'id'=>['neq',$post['id']]])->select();
                if(!empty($name)) {
                    $this->error('提交失败：该角色名已存在');
                }
                //处理选中的权限菜单id，转为字符串
                if(!empty($post['admin_menu_id'])) {
                    $post['permissions'] = implode(',',$post['admin_menu_id']);
                } else {
                    $post['permissions'] = '0';
                }
                if(false == $model->allowField(true)->save($post,['id'=>$id])) {
                    $this->error('修改失败');
                } else {

                     $this->success('修改角色信息成功','admin/role/index');
                }
            } else {
                //非提交操作
                $info['cate'] = $model->where('id',$id)->find();
                if(!empty($info['cate']['permissions'])) {
                    //将菜单id字符串拆分成数组
                    $info['cate']['permissions'] = explode(',',$info['cate']['permissions']);
                }
                $menus = Db::name('authmenu')->select();
                $info['menu'] = $this->mblist($menus);
                $this->assign('info',$info);
                return $this->fetch();
            }
        } else {
            //是新增操作
            if($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['name', 'require', '角色名称不能为空'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                //验证用户名是否存在
                $name = $model->where('name',$post['name'])->find();
                if(!empty($name)) {
                    $this->error('提交失败：该角色名已存在');
                }
                //处理选中的权限菜单id，转为字符串
                if(!empty($post['admin_menu_id'])) {
                    $post['permissions'] = implode(',',$post['admin_menu_id']);
                }
                if(false == $model->allowField(true)->save($post)) {
                    $this->error('添加角色失败');
                } else {

                    $this->success('添加角色成功','admin/role/index');
                }
            } else {
                //非提交操作
                $menus = Db::name('authmenu')->select();
                $info['menu'] = $this->mblist($menus);

                $this->assign('info',$info);
                return $this->fetch();
            }
        }
    }
}